﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Entities;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.EntityComponents;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using GameEngine.Camera;

namespace PvT.Entities
{
    public class EnemyEntity : AbstractDrawableEntity
    {
        #region Fields

        private int singleImageWidth = 60;
        private int singleImageHeight = 96;

        #endregion
        #region Properties

        /// <summary>
        /// Gets or sets the bounding rectangle.
        /// </summary>
        /// <value>
        /// The bounding rectangle.
        /// </value>
        public override Rectangle BoundingRectangle
        {
            get { return new Rectangle((int)position.X, (int)position.Y, (int)(singleImageWidth * scale), (int)(singleImageHeight * scale)); }
            set { base.BoundingRectangle = value; }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="EnemyEntity" /> class.
        /// </summary>
        /// <param name="components">The components.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        /// <param name="position">The position.</param>
        /// <param name="game">The game.</param>
        public EnemyEntity(List<IEntityComponent> components, bool isActive, bool isVisible, Vector2 position, Game game) :
            base(components, isActive, isVisible, position, game)
        {
            this.scale = 0.2f;

            if (isActive == true)
            {
                components.Add(new MoveableComponent(this, 0.1f, true));
                components.Add(new EnemyComponent(this));
                components.Add(new CollidableComponent(this, 0.1f));
                components.Add(new TerminateableComponent(this));
            }
        }

        /// <summary>
        /// Loads the content.
        /// </summary>
        public override void LoadContent()
        {
            base.texture = game.Content.Load<Texture2D>("Enemy/terminator");
        }

        /// <summary>
        /// Updates the enemyEntity
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            CollidableComponent collide = (CollidableComponent)this.GetComponent(typeof(CollidableComponent));

            if (this.GetComponent(typeof(MoveableComponent)) != null)
            {
                MoveableComponent moveable = (MoveableComponent)this.GetComponent(typeof(MoveableComponent));

                if (moveable.movementDirection == MoveableComponent.MoveDir.Forward)
                    effects = SpriteEffects.None;
                else if (moveable.movementDirection == MoveableComponent.MoveDir.Backward)
                    effects = SpriteEffects.FlipHorizontally;
            }

            /*spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null,
                        ((Camera2D)game.Services.GetService(typeof(Camera2D))).GetTransformation());

            spriteBatch.Draw(texture, new Vector2(BoundingRectangle.Left, BoundingRectangle.Top),
                new Rectangle(BoundingRectangle.Left, BoundingRectangle.Top, 1, BoundingRectangle.Height), Color.Black);
            spriteBatch.End();*/

            base.Draw(spriteBatch, gameTime);
        }

        /// <summary>
        /// Sets the scale.
        /// </summary>
        /// <param name="scale">The scale.</param>
        public void SetScale(float scale)
        {
            this.scale = scale;
        }
    }
}
